package com.kxc.controller;

import com.kxc.domain.KxcMessage;
import com.kxc.domain.KxcResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

/**
 * Created by kk on 2017/8/9.
 */
@Controller
public class KxcController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @MessageMapping("/welcome")
    @SendTo("/topic/getResponse")
    public KxcResponse say(KxcMessage message) throws  Exception{
        Thread.sleep(1000);  //当前线程休眠1秒
        return new KxcResponse("welcome "+message.getName());
    }
    @MessageMapping("/chat")
    public void handleChat(Principal principal,String msg){
        if(principal.getName().equals("kxc")){
            messagingTemplate.convertAndSendToUser("kk",
                    "/queue/notifications",principal.getName()
            +"-send:"+msg);
        }else {
            messagingTemplate.convertAndSendToUser("kxc",
                    "/queue/notifications",principal.getName()
            +"-send"+msg);
        }
    }
}
